<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { ElButton, ElMessage } from 'element-plus'

import { Table } from '@/components/Table'
// import { delTableListApi } from '@/api/table'
import { useTable } from '@/hooks/web/useTable'
import { CustomsClearanceFormType } from '@/api/common/types'
import { customs_orderIndex, customs_orderDel } from '@/api/common'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { ref, reactive } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import { Dialog } from '@/components/Dialog'
import Detail from './components/Detail.vue'
const { t } = useI18n()

import { useAppStoreWithOut } from '@/store/modules/app'
import { useCache } from '@/hooks/web/useCache'
const appStore = useAppStoreWithOut()
const { wsCache } = useCache()
//用户权限数组
const userRlues = wsCache.get(appStore.getUserInfo).rlues
const userId = wsCache.get(appStore.getUserInfo).id

const checkRlues = (rlues) => {
  //检查权限
  if (userRlues.includes(rlues)) {
    return true
  } else {
    ElMessage.error('当前用户无权限操作，请联系管理员')
    return false
  }
}

const { register, tableObject, methods } = useTable<CustomsClearanceFormType>({
  getListApi: customs_orderIndex,
  delListApi: customs_orderDel,
  response: {
    list: 'data',
    total: 'total'
  },
  defaultParams: {
    title: ''
  },
  find: userRlues.includes('201') ? -1 : userId
})
const { push } = useRouter()
const { getList, setSearchParams } = methods

getList()
useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index',
    form: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'status',
    label: t('invoice.documentStatus'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'batchno',
    label: t('customsClearanceForm.batchNumber'),
    search: {
      show: true
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'transport',
    label: t('customClearanceForm.modeOfTransport'),
    search: {
      show: true
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'customsName',
    label: t('router.CustomsClearanceFormCompany'),
    search: {
      show: true
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'starTime',
    label: t('customClearanceForm.fromCustomsClearanceDate'),
    search: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        type: 'date'
      }
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'units',
    label: t('invoice.totalNumberOfpices'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'volume',
    label: t('customsClearanceForm.totalVolume'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'quantity',
    label: t('invoice.totalVolume'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'density',
    label: t('customsClearanceForm.totalDensity'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'export_weight',
    label: t('customsClearanceForm.transferOutWeight'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'export_volume',
    label: t('customsClearanceForm.transferOutVolume'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'export_density',
    label: t('customsClearanceForm.transferOutDensity'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'total_fee',
    label: t('customsClearanceForm.transferOutAmountOfMoney'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'price',
    label: t('customsClearanceForm.transferOutOfPrice'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'exterior_mobile',
    label: t('customsClearanceForm.MakeAPhoneCall'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'exterior_user',
    label: t('customsClearanceForm.doNotHandlePersonnel'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'internal_charge',
    label: t('customsClearanceForm.totalDomesticExpenses'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'foreign_charge',
    label: t('customsClearanceForm.totalForeignExpenses'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'margin',
    label: t('customsClearanceForm.totalGrossProfit'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'pledge',
    label: t('customsClearanceForm.warehouseName'),
    search: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      show: true
    }
  },
  {
    field: 'action',
    width: '260px',
    label: t('formDemo.operate'),
    form: {
      show: true
    },
    fixed: 'right',
    detail: {
      show: false
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)
const AddAction = (rules) => {
  if (checkRlues(rules)) push('/logistics/CustomsClearanceFormAdd')
}

// const AddAction = () => {
//   dialogTitle.value = t('exampleDemo.add')
//   tableObject.currentRow = null
//   dialogVisible.value = true
//   actionType.value = ''
// }

const delLoading = ref(false)

const delData = async (row: CustomsClearanceFormType | null, multiple: boolean, rules: string) => {
  if (!checkRlues(rules)) return false
  tableObject.currentRow = row
  const { delList, getSelections } = methods
  const selections = await getSelections()
  delLoading.value = true
  await delList(
    multiple ? selections.map((v) => v.id) : [tableObject.currentRow?.id as string],
    multiple
  ).finally(() => {
    delLoading.value = false
  })
}
const dialogTitle = ref('')
const dialogVisible = ref(false)

const action = (row: CustomsClearanceFormType, type: string, rules: string) => {
  if (!checkRlues(rules)) return false
  switch (type) {
    case 'detail':
      {
        // dialogTitle.value = '详情'
        // dialogVisible.value = true
        // tableObject.currentRow = row
        push(`/logistics/CustomsClearanceFormEdit?id=${row.id}&isDetail=true`)
      }
      break
    case 'Edit':
      {
        tableObject.currentRow = row
        push(`/logistics/CustomsClearanceForm${type}?id=${row.id}`)
      }
      break
    default:
      break
  }
}
</script>

<template>
  <ContentWrap>
    <Search
      :model="{}"
      :schema="allSchemas.searchSchema"
      @search="setSearchParams"
      @reset="setSearchParams"
    />

    <div class="mb-10px">
      <!-- <ElButton :loading="delLoading" type="primary" @click="delData(null, true, '26-6')">
        {{ t('customClearanceForm.import') }}
      </ElButton> -->
      <ElButton type="primary" @click="AddAction('26-1')">{{ t('exampleDemo.add') }}</ElButton>
      <ElButton :loading="delLoading" type="danger" @click="delData(null, true, '26-3')">
        {{ t('exampleDemo.del') }}
      </ElButton>
    </div>

    <Table
      v-model:pageSize="tableObject.limit"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
      <template #action="{ row }">
        <ElButton type="primary" @click="action(row, 'Edit', '26-2')">
          {{ t('exampleDemo.edit') }}
        </ElButton>
        <ElButton type="success" @click="action(row, 'detail', '26-4')">
          {{ t('router.watch') }}
        </ElButton>
        <ElButton type="danger" @click="delData(row, false, '26-3')">
          {{ t('exampleDemo.del') }}
        </ElButton>
      </template>
    </Table>
  </ContentWrap>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <Detail :detail-schema="allSchemas.detailSchema" :current-row="tableObject.currentRow" />
  </Dialog>
</template>
